<template>
  <div class="list">

    <!-- 添加按钮 -->
    <el-button type="primary" class="btn-class" size="mini" @click="openAddDialog">添加</el-button>

    <!-- 表格 -->
    <el-table :data="linkList" border style="width: 100%">
      <el-table-column prop="linkId" label="编号" />
      <el-table-column prop="linkName" label="名称" />
      <el-table-column prop="linkUrl" label="链接地址" />
      <el-table-column prop="createdTime" label="创建时间" />
      <el-table-column prop="updateTime" label="修改时间" />

      <!-- 操作 -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row.linkId)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.linkId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加、修改操作 -->
    <el-dialog title="添加" :visible.sync="drawerAdd">
      <link-add @closeAddDialog="closeAddDialog" @getLinkList="getLinkList" />
    </el-dialog>

    <el-dialog title="修改" :visible.sync="drawerUpdate">
      <link-update :link="link" @closeUpdateDialog="closeUpdateDialog" @getLinkList="getLinkList" />
    </el-dialog>

  </div>
</template>
<script>

import linkApi from '@/api/link'
import LinkAdd from './link-add'
import linkUpdate from './link-update'
export default {
  name: 'LinkList',
  components: {
    LinkAdd,
    linkUpdate
  },
  data() {
    return {
      link: {},
      linkList: [], // link列表
      drawerUpdate: false, // 修改
      drawerAdd: false // 添加
    }
  },
  // 钩子函数，实例化之前执行
  created() {
    this.getLinkList()
  },
  methods: {
    // 获取分页信息
    getLinkList() {
      linkApi.getList().then(res => {
        console.log(res.data)
        this.linkList = res.data
      })
    },
    // 修改
    handleEdit(id) {
      linkApi.get(id).then(res => {
        this.link = res.data
        this.drawerUpdate = true
      })
    },
    // 删除
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除接口
        linkApi.deleteById(id).then(res => {
          this.$message.success(res.msg)
          // 刷新表格
          this.getLinkList()
        })
      })
    }, // 打开添加
    openAddDialog() {
      this.drawerAdd = true
    }, // 关闭打开方法
    closeAddDialog() {
      this.drawerAdd = false
    },
    closeUpdateDialog() {
      this.drawerUpdate = false
    }

  }
}
</script>
